package com.cqeec.demo.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.cqeec.demo.bean.AdminUser;
import com.cqeec.demo.service.AdminUserService;
import com.cqeec.demo.util.MD5;
import com.cqeec.demo.util.ValidateUtil;

/**
 * 后台登录管理控制器类
 * 
 * @Title: AdminUserController
 * @Descripton: 处理后台系统用户登录操作
 */
@Controller
@RequestMapping("/")
public class AdminLoginController {
	// 用户
	@Resource(name = "adminUserService")
	private AdminUserService adminUserService;
	@RequestMapping("/admin_login")
	public String login(HttpServletRequest request, @ModelAttribute("adminUser") AdminUser adminUser, 
			@RequestParam(required = true, defaultValue = "") String verify) {
        
		Map<String, String> result = new HashMap<>(0);
		result.clear();
		
		// 校验登录名或密码
		if (!ValidateUtil.isValidate(adminUser.getUsername())) {
			result.put("username", "请输入登录名！");
		}
		if (!ValidateUtil.isValidate(adminUser.getPassword())) {
			result.put("password", "请输入登录密码！");
		}
		
		// 获取Session对象
		HttpSession session = request.getSession();
		
		// 校验Session对象中是否存在验证码
		String verifyCode = (String) session.getAttribute("VERIFYCODE");
		if (!ValidateUtil.isValidate(verifyCode) || !ValidateUtil.isValidate(verify)) {
			result.put("verify", "请重新获取验证码！");
		} else if (!verifyCode.equalsIgnoreCase(verify)) {
			result.put("verify", "输入的验证码不对！");
		}
		
		// 返回错误信息
		if(!result.isEmpty()) {
			request.setAttribute("msg", result);
			request.setAttribute("username", adminUser.getUsername());
			request.setAttribute("verify", verify);
			return "login";
		}
		
		// 处理登录
		try {
			AdminUser suserInfo = adminUserService.login(adminUser.getUsername(), MD5.MD5Encode(adminUser.getPassword()));
			if (null == suserInfo) {
				result.put("error", "账号或密码错误！");
				request.setAttribute("msg", result);
				request.setAttribute("username", adminUser.getUsername());
				request.setAttribute("verify", verify);
				return "login";
			} else if (suserInfo.getClose() != 0) {
				result.put("error", "账号被锁定！");
				request.setAttribute("msg", result);
				request.setAttribute("username", adminUser.getUsername());
				request.setAttribute("verify", verify);
				return "login";
			}
			
            // 把登录信息写入Session对象中
			session.setAttribute("system_admin_login_info", suserInfo);
            String mysqlVersion = adminUserService.getMysqlVersion();
            session.setAttribute("mysqlVersion", mysqlVersion);
			
			return "redirect:main.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			result.put("error", "登录出错！");
			request.setAttribute("msg", result);
			request.setAttribute("username", adminUser.getUsername());
			request.setAttribute("verify", verify);
			return "login";
		}
	}
	
	@RequestMapping("/login_out")
	public String adminLoginOut(HttpServletRequest request) {
		HttpSession session = request.getSession();
		session.removeAttribute("system_admin_login_info");
		session.removeAttribute("user_role_auth");
		session.invalidate();
		return "redirect:login.jsp";
	}
	
}
